//
//  LGUploadReportPatientController.m
//  HeartDisease_YHJK
//
//  Created by 1234 on 2025/9/11.
//  报告上传 -- 患者列表

#import "LGUploadReportPatientController.h"
#import "LGMedicineBallDetailController.h"
#import "LGUploadReportPatientCell.h"
@interface LGUploadReportPatientController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *keyword;

@end

@implementation LGUploadReportPatientController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.keyword = @"";
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor colorWithString:@"#F2F3F5"];
    [self createNavigationView];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.tableView frame:CGRectMake(0, 0, Screen_W, self.tableView.bounds.size.height)];
    self.emptyView.backgroundColor = [UIColor colorWithString:@"#F2F3F5"];
    [self requestData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.isfirst == NO) {
        [self requestData];
    }
}

-(void)requestData{
    NSString *keyword = (_keyword && ![_keyword isEmptyString])?_keyword:@"";
    [RequestUtil withPOST:@"/doctor/Careplan/listByreport" parameters:@{@"keyword":keyword} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    }];
}


-(void)textFieldEditingChanged:(UITextField *)textField{
    NSString *lang = textField.textInputMode.primaryLanguage;// 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textField markedTextRange];
        if (!selectedRange) { // 没有高亮
            self.keyword = textField.text;
            [self requestData];
        }
    }else{
        self.keyword = textField.text;
        [self requestData];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGUploadReportPatientCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    LGMedicineBallDetailController *controller = [[LGMedicineBallDetailController alloc] init];
    controller.operatName = LGNSString(tempDic[@"task_type_name"]);
    controller.planType = LGNSString(tempDic[@"task_type"]);
    controller.planid = LGNSString(tempDic[@"id"]);
    controller.patientid = LGNSString(tempDic[@"patient_id"]);
    controller.levelid = LGNSString(tempDic[@"level_id"]);
    [self.navigationController pushViewController:controller animated:YES];
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight+viewPix(44), Screen_W, Screen_H-topBarHeight-viewPix(44)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGUploadReportPatientCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithString:@"#F2F3F5"];
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 1)];
    }
    return _tableView;
}

-(void)createNavigationView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, topBarHeight+viewPix(44))];
    baseView.backgroundColor = [UIColor colorWithString:@"#E5F3FF"];
    [self.view addSubview:baseView];
    CAGradientLayer *gradLayer = [[CAGradientLayer alloc]init];
    gradLayer.frame = CGRectMake(0, topBarHeight, Screen_W ,viewPix(44));
    gradLayer.startPoint = CGPointMake(0.5, 0);
    gradLayer.endPoint = CGPointMake(0.5, 1.0);
    gradLayer.colors = @[(__bridge id)[UIColor colorWithString:@"#E5F3FF"].CGColor, (__bridge id)[UIColor colorWithString:@"#F3F9FE"].CGColor, (__bridge id)[UIColor colorWithString:@"#FEFEFF"].CGColor];
    gradLayer.locations = @[@(0), @(0.6), @(1.0f)];
    [baseView.layer addSublayer:gradLayer];
    UIButton *backBtn = [UIButton buttonWithImage:@"navBack" selectImage:@"navBack"];
    backBtn.frame = CGRectMake(0, statusBarHeight, 44, 44);
    [backBtn addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:backBtn];
    UILabel *titleLabel = [UILabel lableWithText:@"报告上传" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightSemibold) textAlignment:NSTextAlignmentCenter lines:1];
    titleLabel.frame = CGRectMake(viewPix(100), statusBarHeight, Screen_W-viewPix(200), 44);
    [baseView addSubview:titleLabel];
    
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(32), viewPix(32))];
    UIImageView *iconView = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(8), viewPix(8), viewPix(16), viewPix(16))];
    iconView.image = [UIImage imageNamed:@"searchGray16"];
    [leftView addSubview:iconView];
    
    UITextField *searchTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(12), topBarHeight+viewPix(6), Screen_W-viewPix(24), viewPix(32))];
    searchTF.backgroundColor = [UIColor colorWithString:@"#F3F3F3"];
    searchTF.tintColor = [UIColor colorWithString:@"#666666"];
    searchTF.textColor = [UIColor colorWithString:@"#666666"];
    searchTF.textAlignment = NSTextAlignmentLeft;
    searchTF.placeholder = @"输入患者关键信息搜索";
    searchTF.font = LGFont(15);
    searchTF.cornerRidus = 5;
    searchTF.leftViewMode = UITextFieldViewModeAlways;
    searchTF.leftView = leftView;
    [searchTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    [baseView addSubview:searchTF];
}

@end
